package com.yezao.yzrouter

import android.util.ArrayMap
import android.util.Log
import java.lang.IllegalArgumentException

object RouterTable {

    private val registedRouteMap = ArrayMap<String, String>()
    fun registRoutes(collection: Map<String, String>) {
        //首先检查key是否重复
        synchronized(this) {
            for (key in collection.keys) {
                if (registedRouteMap.containsKey(key)) {
                    //如果是已经存在的key 要对比是否注册的是同一个对象 如果不是 抛出异常
                    val registedClazz = registedRouteMap[key]
                    val newClazz = collection[key]

                    if (registedClazz == newClazz) {
                        throw IllegalArgumentException("已经使用 key [$key] 注册了一个对象 [${registedClazz.toString()}]")
                    } else {
                        Log.e(YZRouter.TAG, "registRoutes: 重复注册 $key ${registedClazz.toString()}")
                    }
                } else {
                    if (BuildConfig.DEBUG) {
                        println("router registing $key  ${collection[key]}")
                    }
                    registedRouteMap[key] = collection[key]
                }
            }
        }
    }


    fun getLocalClass(route: String): String? {
        if (BuildConfig.DEBUG) {
            println("target " + registedRouteMap[route])
        }
        return registedRouteMap[route]
    }


}